package com.sgb.zhaocai.viewmodel;

import android.text.TextUtils;

import androidx.lifecycle.ViewModel;

import com.sgb.zhaocai.base.BaseApi;
import com.sgb.zhaocai.entity.DemandForecastListBean;
import com.sgb.zhaocai.entity.DemandForecastListEntity;
import com.sgb.zhaocai.view.demand.fragment.WebDemandForecastFragment;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.List;

import retrofit2.Call;

/**
 * Time:2021/9/1
 * AUTHOR:F_Q
 * Description:
 */
public class WebDemandForecastModel extends ViewModel {
    private boolean isLastPage;//是否可以加载更多
    private String longitude = "";
    private String latitude = "";

    /**
     * 列表shuju
     * @param fragment
     * @param position
     * @param requestBean
     */
    public void getMaterialHomeList(WebDemandForecastFragment fragment, int position, DemandForecastListBean requestBean) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        Call<BaseEntity<ListResponse<List<DemandForecastListEntity>>>> call = null;
        switch (position) {
            case 0:
                call = BaseApi.getApi().getMaterialHomeList(requestBean);
                break;
            case 1:
                call = BaseApi.getApi().getMechanicalHomeList(requestBean);
                break;
            case 2:
                call = BaseApi.getApi().getLaborHomeList(requestBean);
                break;
            case 3:
                call = BaseApi.getApi().getSubcontractPlanHomeList(requestBean);
                break;
        }
        call.enqueue(new ICallBack<BaseEntity<ListResponse<List<DemandForecastListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<DemandForecastListEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    isLastPage = data.getData().getCurrentPage() >= data.getData().getTotal();
                    fragment.setDemandList(data.getData().getList(),isLastPage);
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }

    /**
     * 详情
     * @param fragment
     * @param id
     * @param moduleType
     */
    public void getWebH5(WebDemandForecastFragment fragment, String id, String moduleType) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        longitude = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
        latitude = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
        if (TextUtils.isEmpty(longitude)) {
            longitude = "0";
        }
        if (TextUtils.isEmpty(latitude)) {
            latitude = "0";
        }
        BaseApi.getApi().getUrlSetting("engineeringProject").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    String webUrl = data.getData().getUrl() + "?id=" + id + "&lng=" + longitude + "&lat=" + latitude + "&type=" + moduleType;
                    BaseApi.startWeb(fragment.getContext(), webUrl);
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }
}
